<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<!DOCTYPE html>
<html>
<head>

</head>
<body class="no-skin">
	<div class="main-content">
		<!-- #section:basics/content.breadcrumbs -->
		<div class="breadcrumbs" id="breadcrumbs">
			<script type="text/javascript">
				try {
					ace.settings.check('breadcrumbs', 'fixed')
				} catch (e) {
				}
			</script>

			<ul class="breadcrumb">
				<li><i class="ace-icon fa fa-home home-icon"></i> <a href="#">首页</a>
				</li>
				<li class="active">教师菜单</li>
				<li class="active">科目管理</li>
			</ul>
			<!-- /.breadcrumb -->

			<!-- #section:basics/content.searchbox -->
			<div class="nav-search" id="nav-search">
				<form class="form-search">
					<span class="input-icon"> <input type="text"
						placeholder="Search ..." class="nav-search-input"
						id="nav-search-input" autocomplete="off" /> <i
						class="ace-icon fa fa-search nav-search-icon"></i>
					</span>
				</form>
			</div>
			<!-- /.nav-search -->

			<!-- /section:basics/content.searchbox -->
		</div>

		<!-- /section:basics/content.breadcrumbs -->
		<div class="page-content">
			<!-- #section:settings.box -->
			<div class="ace-settings-container" id="ace-settings-container">
				<div class="btn btn-app btn-xs btn-warning ace-settings-btn"
					id="ace-settings-btn">
					<i class="ace-icon fa fa-cog bigger-150"></i>
				</div>

				<div class="ace-settings-box clearfix" id="ace-settings-box">
					<div class="pull-left width-50">
						<!-- #section:settings.skins -->
						<div class="ace-settings-item">
							<div class="pull-left">
								<select id="skin-colorpicker" class="hide">
									<option data-skin="no-skin" value="#438EB9">#438EB9</option>
									<option data-skin="skin-1" value="#222A2D">#222A2D</option>
									<option data-skin="skin-2" value="#C6487E">#C6487E</option>
									<option data-skin="skin-3" value="#D0D0D0">#D0D0D0</option>
								</select>
							</div>
							<span>&nbsp; Choose Skin</span>
						</div>

						<!-- /section:settings.skins -->

						<!-- #section:settings.navbar -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-navbar" /> <label class="lbl"
								for="ace-settings-navbar"> Fixed Navbar</label>
						</div>

						<!-- /section:settings.navbar -->

						<!-- #section:settings.sidebar -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-sidebar" /> <label class="lbl"
								for="ace-settings-sidebar"> Fixed Sidebar</label>
						</div>

						<!-- /section:settings.sidebar -->

						<!-- #section:settings.breadcrumbs -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-breadcrumbs" /> <label class="lbl"
								for="ace-settings-breadcrumbs"> Fixed Breadcrumbs</label>
						</div>

						<!-- /section:settings.breadcrumbs -->

						<!-- #section:settings.rtl -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-rtl" /> <label class="lbl"
								for="ace-settings-rtl"> Right To Left (rtl)</label>
						</div>

						<!-- /section:settings.rtl -->

						<!-- #section:settings.container -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-add-container" /> <label class="lbl"
								for="ace-settings-add-container"> Inside <b>.container</b>
							</label>
						</div>

						<!-- /section:settings.container -->
					</div>
					<!-- /.pull-left -->

					<div class="pull-left width-50">
						<!-- #section:basics/sidebar.options -->
						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-hover" /> <label class="lbl"
								for="ace-settings-hover"> Submenu on Hover</label>
						</div>

						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-compact" /> <label class="lbl"
								for="ace-settings-compact"> Compact Sidebar</label>
						</div>

						<div class="ace-settings-item">
							<input type="checkbox" class="ace ace-checkbox-2"
								id="ace-settings-highlight" /> <label class="lbl"
								for="ace-settings-highlight"> Alt. Active Item</label>
						</div>

						<!-- /section:basics/sidebar.options -->
					</div>
					<!-- /.pull-left -->
				</div>
				<!-- /.ace-settings-box -->
			</div>
			<!-- /.ace-settings-container -->

			<!-- /section:settings.box -->
			<div class="page-content-area">
	
				<div class="row">
				<div class="hr hr-15 dotted hr-double"></div>
					<div id="subjectList">
						<div id="messageDiv">
							<c:if test="${not empty message}">
								<div class="alert alert-success alert-dismissible" role="alert">
									<button type="button" class="close" data-dismiss="alert"
										aria-label="Close">
										<span aria-hidden="true">&times;</span>
									</button>
									<strong class="center">${message}!</strong>
								</div>
							</c:if>
						</div>
						<!-- 管理学生科目模态框 -->
						<div class="modal fade" id="subjectStudentsModal" tabindex="-1"
							role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
							<div class="modal-dialog">
								<div class="modal-content">
									<div class="modal-header">
										<button type="button" class="close" data-dismiss="modal"
											aria-hidden="true">&times;</button>
										<h4 class="modal-title" id="myModalLabel">管理科目学生：<label id="subjectTitle"></abel></h4>
									</div>
									<div class="modal-body">
										<input type="hidden" id="subjectId"/>
										<div id="subjectStudentContent" class="control-group">
										
										</div>
									</div>
									<div class="modal-footer">
										<button type="button" class="btn btn-default subjectStudentSaveBtn"
											data-dismiss="modal">保存</button>
										<button type="button" class="btn btn-default"
											data-dismiss="modal">关闭</button>
									</div>
								</div>
								<!-- /.modal-content -->
							</div>
							<!-- /.modal -->
						</div>
						<!-- 添加科目模态框 -->
						<div class="modal fade" id="addSubjectModal" tabindex="-1"
							role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
							<div class="modal-dialog">
								<div class="modal-content">
									<div class="modal-header">
										<button type="button" class="close" data-dismiss="modal"
											aria-hidden="true">&times;</button>
										<h4 class="modal-title" id="myModalLabel">添加科目</h4>
									</div>
									<form id="addSubjectForm" class="subjectForm" method="post">
										<div class="modal-body">
											<p>
												<input type="text" class="subId" name="subId"  hidden="true"/>
											</p>
											<p>
												<label>科目名称：&nbsp;&nbsp;</label> 
												<input class="subName" name="subName"  placeholder="&nbsp;&nbsp;请填写科目名称"/>
												
											</p>
											<p>
												<label>科目简介：&nbsp;&nbsp;</label>
												<textarea type="text" class="description" name="description" placeholder="&nbsp;&nbsp;请填写科目简介"
													cols="50" rows="9" style="resize:none;overflow-x:hidden;"></textarea>
											</p>
											<p> 
												<label>科目题数：&nbsp;&nbsp;</label><input type="text"
													name="testCount" class="testCount" placeholder="&nbsp;&nbsp;题目数量" style="width:100px;">
											</p>
											<p>
												<label>总&nbsp;时&nbsp;长&nbsp;：&nbsp;&nbsp;&nbsp;</label><input type="text"
													name="totalTime" class="totalTime" placeholder="&nbsp;&nbsp;分钟" style="width:100px;">
											</p>
											<p>
												<label>满分总分：&nbsp;&nbsp;</label><input type="text"
													name="totalScore" class="totalScore" placeholder="&nbsp;&nbsp;总分值" style="width:100px;">
											</p>
											<p>
												<label>是否开启考试：&nbsp;&nbsp;</label>											
												<input id="id-button-borders"  type="checkbox" name="status"
												 class="ace ace-switch ace-switch-5 statusBtn" value="${subject.status}" >
												<span class="lbl middle"></span>
											</p>
										</div>
										<div class="modal-footer">
											<button type="button" class="btn btn-info saveType"
													data-dismiss="modal">提交</button>
											<button type="button" class="btn btn-default"
													data-dismiss="modal">关闭</button>
										</div>
									</form>
								</div>
								<!-- /.modal-content -->
							</div>
							<!-- /.modal -->
						</div>
						<!-- 修改科目模态框 -->
						<div class="modal fade" id="editSubjectModal" tabindex="-1"
							role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
							<div class="modal-dialog">
								<div class="modal-content">
									<div class="modal-header">
										<button type="button" class="close" data-dismiss="modal"
											aria-hidden="true">&times;</button>
										<h4 class="modal-title" id="myModalLabel">修改科目</h4>
									</div>
									<form id="editSubjectForm" class="subjectForm" method="post">
										<div class="modal-body">
											<p>
												<input type="text" class="subId" name="subId" hidden="true" />
											</p>
											<p>
												<label>科目名称：&nbsp;&nbsp;</label> 
												<input class="subName" name="subName"  placeholder="&nbsp;&nbsp;请填写科目名称"/>
												
											</p>
											<p>
												<label>科目简介：&nbsp;&nbsp;</label>
												<textarea type="text" class="description" name="description" placeholder="&nbsp;&nbsp;请填写科目简介"
													cols="50" rows="9" style="resize:none;overflow-x:hidden;"></textarea>
											</p>
											<p> 
												<label>科目题数：&nbsp;&nbsp;</label><input type="text"
													name="testCount" class="testCount" placeholder="&nbsp;&nbsp;题目数量" style="width:100px;">
											</p>
											<p>
												<label>总&nbsp;时&nbsp;长&nbsp;：&nbsp;&nbsp;&nbsp;</label><input type="text"
													name="totalTime" class="totalTime" placeholder="&nbsp;&nbsp;分钟" style="width:100px;">
											</p>
											<p>
												<label>满分总分：&nbsp;&nbsp;</label><input type="text"
													name="totalScore" class="totalScore" placeholder="&nbsp;&nbsp;总分值" style="width:100px;">
											</p>
											<p>
												<label class="ps">是否开启考试：&nbsp;&nbsp;</label>											
												<span class="lbl middle"></span>
											</p>
										</div>
										<div class="modal-footer">
											<button type="button" class="btn btn-info saveType"
													data-dismiss="modal">提交</button>
											<button type="button" class="btn btn-default"
													data-dismiss="modal">关闭</button>
										</div>
									</form>
								</div>
								<!-- /.modal-content -->
							</div>
							<!-- /.modal -->
						</div>
						<table id="sample-table-1"
							class="table table-striped table-bordered table-hover">
							<thead>
								<tr>
									<td><a href="changeLocale?localeType=en_US"><spring:message
												code="page.index.english" /></a></td>
									<td><a href="changeLocale?localeType=zh_CN"><spring:message
												code="page.index.chinese" /></a></td>
								</tr>
								<button class="btn btn-success btn-xs addSubjectModalBtn">
											<i class="glyphicon glyphicon-plus "></i>
										</button>
								<tr>
									<th><spring:message code="subject.id.message" /></th>
									<th><spring:message code="subject.name.message" /></th>
									<th><spring:message code="subject.description.message" /></th>
									<th><spring:message code="subject.test_count.message" /></th>
									<th><spring:message code="subject.total_time.message" /></th>
									<th><spring:message code="subject.total_score.message" /></th>
									<th><spring:message code="subject.status.message" /></th>
									<th class="hidden-480"><spring:message
											code="page.action.message" /></th>
								</tr>
							</thead>
							<tbody>
								<c:forEach items="${subjectList}" var="subject">
									<tr>
										<td>${subject.id}</td>
										<td>${subject.name}</td>
										<td>${subject.description}</td>
										<td>${subject.test_count }</td>
										<td>${subject.total_time }</td>
										<td>${subject.total_score}</td>
										<td>
											<input id="id-button-borders" type="checkbox" name="statusTd"
											 class="ace ace-switch ace-switch-5 statusTd" value="${subject.status}" 
											 	<c:if test="${subject.status==1}">checked</c:if> >
											<span class="lbl middle"></span>
										</td>
										<td>
											<button title="修改科目" class="btn btn-info btn-xs editSubjectBtn">
												<i class="glyphicon glyphicon-pencil"></i>
											</button>&nbsp;&nbsp;
											<button title="删除科目" class="btn btn-danger btn-xs"
												onclick="del(${subject.id})">
												<i class="glyphicon glyphicon-trash"></i>
											</button>&nbsp;&nbsp;
											<button title="管理科目学生" class="btn btn-info btn-xs sub-stu-btn">
												<i class="glyphicon glyphicon-list-alt"></i>
											</button>
										</td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
					</div>
				</div>
				<!-- /.row -->
			</div>
			<!-- /.page-content-area -->
		</div>
		<!-- /.page-content -->
	</div>
	<script type="text/javascript">
	function del(id) { 
		var con = confirm("是否删除？"); 
		if(con==true){
		location.href="del/"+id; 
		} 
	};
	
	
	function checkSelectedUser(selectedUser,userId){
		for(var id in selectedUser){
			
			if(selectedUser[id]==userId){
				return true;
			}
		}
		return false;
	}
	
	$('.sub-stu-btn').click(function(){
		//获取当前的科目
		var subId=$(this).parents("td").siblings("td").eq(0).text();
		$.ajax({
			type: 'POST', 
			url: '${pageContext.request.contextPath}/teacher/getAllstudents',
			data: {subjectId:subId},
			dataType : "json",
			success : function(data){
				var html='';
				var selectedUser=data.selectedUser;
				//得到所有关联的学生
				for(var i=0;i<data.usersId.length;i++){
					var userId=data.usersId[i];
					var userName=data.usersName[i];
					var checked=checkSelectedUser(selectedUser,userId)?'checked="checked"':'';
					console.log(checkSelectedUser(selectedUser,userId));
					html+='<div class="checkbox">';
					html+='   <label>';
					html+='      <input name="subjectStudents" class="ace ace-checkbox-2" type="checkbox" value="'+userId+'" '+checked+'>';
					html+='      <span class="lbl">'+userName+'</span>';
					html+='   </label>';
					html+='</div>';
				}
				//把所有学生加入到模态框
				$('#subjectStudentContent').html(html); 
				$('#subjectId').val(subId); 
			}
		});
		$('#subjectTitle').text($(this).parents("td").siblings("td").eq(1).text());
		$('#subjectStudentsModal').modal('show');
	});
	
	$('.subjectStudentSaveBtn').click(function(){
		var subjectStudents=[];
		//获取所以已选的学生学号
		for(var i = 0;i<$('input[name="subjectStudents"]:checked').length;i++){
			subjectStudents[i]=$($('input[name="subjectStudents"]:checked')[i]).val();
		}
		$.ajax({
			type:'POST',
			url:'${pageContext.request.contextPath}/teacher/saveSubjectStudents',
			data:{subjectStudents:subjectStudents,
				  subjectId:$('#subjectId').val()},
				  
			dataType:'json',
			success:function(data){
				//判断是否成功
				if(data.success){
					alert('成功');
					$('#subjectStudentsModal').modal('hide');
				}else{
					alert('失败');
				}
			}
		})
	});
	
	$('.addSubjectModalBtn').click(function(){
		$('#addSubjectModal').modal('show');
	});
	
	$('#addSubjectForm .saveType').click(function(){
		saveSubject('add','addSubjectForm');
	});
	
	$('.editSubjectBtn').click(function(){
		var id=$(this).parents("td").siblings("td").eq(0).text();
		var name=$(this).parents("td").siblings("td").eq(1).text();
		var description=$(this).parents("td").siblings("td").eq(2).text();
		var test_count=$(this).parents("td").siblings("td").eq(3).text();
		var total_time=$(this).parents("td").siblings("td").eq(4).text();
		var total_score=$(this).parents("td").siblings("td").eq(5).text();
		var status=$(this).parents("td").siblings("td").eq(6).children("input")[0].value;
		
		
		$('#editSubjectForm .subId').val(id);
		$('#editSubjectForm .subName').val(name);
		$('#editSubjectForm .description').val(description);
		$('#editSubjectForm .testCount').val(test_count);
		$('#editSubjectForm .totalTime').val(total_time);
		$('#editSubjectForm .totalScore').val(total_score);
		$('#editSubjectForm input:checkbox').eq(0).val(status); 
		$("#editSubjectForm input:checkbox").remove();
		if(status==1){
			$("#editSubjectForm .ps").after('<input id="id-button-borders"  type="checkbox" value="'+status+'" checked class="ace ace-switch ace-switch-5 statusBtn" >');
		}else{
			$("#editSubjectForm .ps").after('<input id="id-button-borders"  type="checkbox" value="'+status+'" class="ace ace-switch ace-switch-5 statusBtn" >');
		}
		$('#editSubjectModal').modal('show');
	});
	
	$('#editSubjectForm .saveType').click(function(){
		saveSubject('edit','editSubjectForm');
	});
	
	function saveSubject(url,did){
		var id=$('#'+did+' input[name="subId"]').val();
		console.log(id); 
		var name=$('#'+did+' input[name="subName"]').val();
		var description=$('#'+did+' .description').val();
		var test_count=$('#'+did+' input[name="testCount"]').val();
		var total_time=$('#'+did+' input[name="totalTime"]').val();
		var total_score=$('#'+did+' input[name="totalScore"]').val();
		var checked=$('#'+did+' input[name="status"]:checkbox:checked');
		var status=checked.length;
		$.ajax({
			type:"POST",
			url:'${pageContext.request.contextPath}/subject/'+url,
			data:{
				id:id,
				name:name,
				description:description,
				test_count:test_count,
				total_time:total_time,
				total_score:total_score,
				status:status
			},
			dataType:'json',
			success:function(data){
				if(data.success){
					alert("成功");
					window.location.reload();
				}else{
					alert("失败");
				}
			}
		});
	};
	
	$('.statusTd').click(function(){
		var id=$(this).parents("td").siblings("td").eq(0).text();
		var temp=$(this)[0].value;
		var status;
		var con;
		if(temp==1){
			con=confirm("是否关闭考试？")
			status=0;
		}else{
			con=confirm("是否开启考试？")
			status=1;
		}
		console.log(con);
		if(con){
			$.ajax({
				type:'POST',
				url:'${pageContext.request.contextPath}/teacher/changeStatus',
				data:{id:id,
					status:status},
				dataType:'json',
				success:function(data){
					if(data.success){
						window.location.reload();
					}
				}
			});
		}
	});
	
	
	
	
	</script>
</html>
</body>
</html>